// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Service.java

package com.sgame.android.social;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.widget.Toast;
import com.sgame.android.Mobage;
import com.sgame.android.MLog;
import com.sgame.android.Session;
import com.sgame.android.data.MBUParcelableUser;
import com.sgame.android.data.MBUUser;
import com.sgame.android.data.User;
import com.sgame.android.lang.*;
import com.sgame.android.lang.Error;
import com.sgame.android.network.InvalidCredentialsConfigurationException;
import com.sgame.android.network.SgameJsonHttpResponseHandler;
import com.sgame.android.social.IMobageHttpResponseHandler;
import com.sgame.android.social.ui.LoadWebViewActivity;
//import com.sgame.android.social.ui.USFriendPickerActivity;
//import com.sgame.ww.android.util.MBToast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.json.JSONObject;

public final class Service extends CommonAPIBase

{
    public static interface OnFriendPickerComplete
    {

        public abstract void onPicked(ArrayList arraylist);

        public abstract void onInviteSent(ArrayList arraylist);

        public abstract void onDismiss();
    }

    public static interface OnDialogComplete
    {

        public abstract void onDismiss();
    }

    public static interface OnComplete
    {

        public abstract void onSuccess();

        public abstract void onError(Error error);
    }

    public static interface IShowCommunityUICallback
    {

        public abstract void onComplete(DismissableAPIStatus dismissableapistatus, Error error);
    }

    public static interface IOpenUserProfileCallback
    {
        public static enum APIStatus
        {
            dismiss, error;
        }

        public abstract void onComplete(APIStatus apistatus, Error error);
    }

    public static interface IOpenBankDialogCallback
    {

        public abstract void onComplete(SimpleAPIStatus simpleapistatus, Error error);
    }

    public static interface IOpenFriendPickerCallback
    {

        public abstract void onComplete(DismissableAPIStatus dismissableapistatus, Error error, List list, List list1);
    }

    public static interface IExecuteLogoutCallback
    {

        public abstract void onComplete(SimpleAPIStatus simpleapistatus, Error error);
    }

    public static interface IExecuteLoginWithParamsCallback
    {

        public abstract void onComplete(CancelableAPIStatus cancelableapistatus, Error error);
    }

    public static interface IExecuteLoginCallback
    {

        public abstract void onComplete(CancelableAPIStatus cancelableapistatus, Error error);
    }

    public static interface IOpenLoginDialogCallback
    {

        public abstract void onComplete(CancelableAPIStatus cancelableapistatus, Error error, User user);
    }

    public Service()
    {
    }

    public static void openUserProfile(String userId, OnDialogComplete onComplete)
    {
        MLog.e("Service", "openUserProfile not implemented");
        Toast.makeText(Mobage.currentActivity, "openUserProfile not implemented", 1).show();
    }

    public static void openFriendPicker(int maxFriendsToSelect, final OnFriendPickerComplete onComplete)
    {
        	openFriendPicker(Mobage.currentActivity, maxFriendsToSelect, new IMobageHttpResponseHandler.OnFriendPickerResponseHandler() {

            public void onPicked(List friendUserIds)
            {
                onComplete.onPicked((ArrayList)friendUserIds);
            }

            public void onInviteSent(List friendUserIds)
            {
                onComplete.onInviteSent((ArrayList)friendUserIds);
            }

            public void onError(Error error)
            {
                Toast.makeText(Mobage.getCurrentActivity(), error.getDescription(), 0).show();
                onComplete.onDismiss();
            }

            public void onDismiss()
            {
                onComplete.onDismiss();
            }
        }
        );
    }

    public static void showBankUi(final OnDialogComplete onComplete)
    {
    	IMobageHttpResponseHandler.OnDialogResponseHandler responseHandler = new IMobageHttpResponseHandler.OnDialogResponseHandler()
        {
    	   	@Override
            public void onDismiss()
            {
                onComplete.onDismiss();
            }

    	    @Override
            public void onError(Error error)
            {
                Toast.makeText(Mobage.getCurrentActivity(), error.getDescription(), 5).show();
                onComplete.onDismiss();
            }
        };
        android.app.Activity activity = Mobage.getCurrentActivity();
        //Service.openBankDialog(activity, responseHandler);
    }

    public static Bitmap getBalanceImage(Rect rect)
    {
        throw new RuntimeException("Not implemented: getBalanceImage");
    }

    public static void launchPortalApp(OnDialogComplete onComplete)
    {
        throw new RuntimeException("Not implemented: launchPortalApp");
    }
   
    public static void openLoginDialog(Activity activity1, IMobageHttpResponseHandler.OnLoginResponseHandler responseHandler)
    {
        Mobage.openLoginDialog(activity1, responseHandler);
    }

    public static void openLoginDialog(Activity activity1,final IOpenLoginDialogCallback onComplete)
    {
    	Mobage.openLoginDialog(activity1, new IMobageHttpResponseHandler.OnLoginResponseHandler() {

            public final void onCancel()
            {
            	onComplete.onComplete(CancelableAPIStatus.cancel, null, null);
            }

            public final void onComplete(User user)
            {
            	onComplete.onComplete(CancelableAPIStatus.success, null, user);
            }

            public final void onError(Error error)
            {
            	onComplete.onComplete(CancelableAPIStatus.error, error, null);
            }
        }
    	);
    }

    public static void executeLogin(Activity activity1, final IExecuteLoginCallback onComplete)
    {
    	Mobage.openLoginDialog(activity1, new IMobageHttpResponseHandler.OnLoginResponseHandler() {

            public final void onCancel()
            {
            	onComplete.onComplete(CancelableAPIStatus.cancel, null);
            }

            public final void onComplete(User user)
            {
            	onComplete.onComplete(CancelableAPIStatus.success, null);
            }

            public final void onError(Error error)
            {
            	onComplete.onComplete(CancelableAPIStatus.error, error);
            }
        }
    	);
    }

    public static void executeLoginWithParams(Activity activity1, HashMap params, final IExecuteLoginWithParamsCallback onComplete)
    {
    	Mobage.openLoginDialog(activity1, params, new IMobageHttpResponseHandler.OnLoginResponseHandler() {

            public final void onCancel()
            {
            	onComplete.onComplete(CancelableAPIStatus.cancel, null);
            }

            public final void onComplete(User user)
            {
            	onComplete.onComplete(CancelableAPIStatus.success, null);
            }

            public final void onError(Error error)
            {
            	onComplete.onComplete(CancelableAPIStatus.error, error);
            }
        }
    			);
    }

    public static void executeLogout(final IExecuteLogoutCallback onComplete)
    {
        //Mobage.v().a();
		Session session;
		if((session = Mobage.getSession()) != null && session.getCredentials() != null)
		    session.getCredentials().clearTokens();
		/*
		FacebookSession.b(Mobage.n(), new FacebookSession.FBRequestResult() {

		    public final void a(JSONObject jsonobject)
		    {
		        a.onComplete(SimpleAPIStatus.success, null);
		    }

		    public final void a(Error error)
		    {
		        a.onComplete(SimpleAPIStatus.error, error);
		    }

		    public final void a()
		    {
		        a.onComplete(SimpleAPIStatus.error, new Error(ErrorMap.FACEBOOK_CANCEL));
		    }

		    final IExecuteLogoutCallback a;

		
		{
		    a = onComplete;
		}
		}
		);*/
		onComplete.onComplete(SimpleAPIStatus.error, new Error(ErrorMap.INVALID_DATA));
		return;
    }

    public static void openFriendPicker(Activity activity1, int maxFriendsToSelect, final IMobageHttpResponseHandler.OnFriendPickerResponseHandler responseHandler)
    {
        openFriendPicker(activity1, maxFriendsToSelect, new IOpenFriendPickerCallback() {

            public final void onComplete(DismissableAPIStatus status, Error error, List pickedFriends, List inviteFriends)
            {

                switch(status)
                {
                case dismiss: // '\001'
                	responseHandler.onDismiss();
                    return;

                case error: // '\002'
                	responseHandler.onError(error);
                    return;

                case success: // '\003'
                	ArrayList inviteList = new ArrayList();
                	User user;
                	Iterator<User> item;
                    for(item = inviteFriends.iterator(); item.hasNext(); inviteList.add(user.getId()))
                    	user = (User)item.next();

                    ArrayList pickedList = new ArrayList();
                    for(item = pickedFriends.iterator(); item.hasNext(); pickedList.add(user.getId()))
                        user = (User)item.next();

                    responseHandler.onInviteSent(inviteList);
                    responseHandler.onPicked(pickedList);
                    break;
                }
            }
        }
        		);
    }

    public static void openFriendPicker(Activity activity, int maxFriendsToSelect, final IOpenFriendPickerCallback onComplete)
    {
        if(!isLogin())
        {
            onComplete.onComplete(DismissableAPIStatus.error, getErrorLogin(), null, null);
            return;
        }
        if(!Mobage.isConnected())
        {
            onComplete.onComplete(DismissableAPIStatus.error, new Error(ErrorMap.NETWORK_UNAVAILABLE), null, null);
            return;
        }
        if(!mShowed.compareAndSet(false, true))
        {
            onComplete.onComplete(DismissableAPIStatus.error, new Error(ErrorMap.UI_ACTIVITY_ALREADY_SHOWING), null, null);
            return;
        } else
        {
        	/*
            USFriendPickerActivity.a(activity, maxFriendsToSelect, new IOpenFriendPickerCallback() {

                public final void onComplete(DismissableAPIStatus status, Error error, List pickedFriends, List inviteFriends)
                {
                    Service.a.set(false);
                    a.onComplete(status, error, pickedFriends, inviteFriends);
                }

                final IOpenFriendPickerCallback a;

            
            {
                a = onComplete;
            }
            }
            		);
            */		
            return;
        }
    }

    public static void openUserProfile(final Activity activity,final  String userId,final  IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler responseHandler)
    {
        final boolean flag;
        final IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler onopenuserprofileresponsehandler;
        if(!isLogin())
        {
            MLog.e("Service", "Mobage object is not initialized.");
            responseHandler.onError(getErrorInvalidSession());
            return;
        }
        if(!Mobage.isConnected())
        {
            responseHandler.onError(new Error(ErrorMap.NETWORK_UNAVAILABLE));
            return;
        }
        if(!mShowed.compareAndSet(false, true))
        {
            responseHandler.onError(new Error(ErrorMap.UI_ACTIVITY_ALREADY_SHOWING));
            return;
        }
        flag = Mobage.getSession().getUser().getId().equals(userId);
        onopenuserprofileresponsehandler = new IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler() {

            public final void onDismiss()
            {
                Service.mShowed.set(false);
                this.onDismiss();
            }

            public final void onError(Error error)
            {
                Service.mShowed.set(false);
                this.onError(error);
            }
        }
        ;
        if(flag)
        {
        	MBUUser user = (MBUUser)MBUParcelableUser.a(Mobage.getSession().getUser().toJson());
            LoadWebViewActivity.Load(activity, user, flag, null, onopenuserprofileresponsehandler);
            return;
        }
        try {
        	SgameJsonHttpRequestExecute.getCurrentUser(Mobage.createSgameHttpClient(), userId, Mobage.getInstance().getAppKey(), new IMobageHttpResponseHandler.OnGetUserResponseHandler() {

			    public final void onComplete(User user)
			    {
			    	MBUParcelableUser mbuser = MBUParcelableUser.a(Mobage.getSession().getUser().toJson());
			    	LoadWebViewActivity.Load(a, mbuser, b, null, c);
			    }

			    public final void onError(Error error)
			    {
			        d.onError(error);
			    }

			    final Activity a;
			    final boolean b;
			    final IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler c;
			    final IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler d;

			    
			    {
			        a = activity;
			        b = flag;
			        c = onopenuserprofileresponsehandler;
			        d = responseHandler;
			    }
			}
			);
		} catch (InvalidCredentialsConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return;
    }

    public static void openUserProfile(Activity activity1, String userId, final IOpenUserProfileCallback onComplete)
    {
        openUserProfile(activity1, userId, new IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler() {

            public final void onDismiss()
            {
                a.onComplete(IOpenUserProfileCallback.APIStatus.dismiss, null);
            }

            public final void onError(Error error)
            {
                a.onComplete(IOpenUserProfileCallback.APIStatus.error, error);
            }

            final IOpenUserProfileCallback a;
            
            {
                a = onComplete;
            }
        }
);
    }

    public static void openUserProfile(Activity activity1, User user)
    {
        openUserProfile(activity1, user.getId(), new IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler() {

            public final void onDismiss()
            {
            }

            public final void onError(Error error)
            {
            }

        }
        );
    }

    public static void showCommunityUI(Activity activity, final IShowCommunityUICallback responseHandler)
    {
        if(!isLogin())
        {
            MLog.e("Service", "Mobage object is not initialized.");
            responseHandler.onComplete(DismissableAPIStatus.error, getErrorLogin());
            return;
        }
        if(!Mobage.isConnected())
        {
            responseHandler.onComplete(DismissableAPIStatus.error, new Error(ErrorMap.NETWORK_UNAVAILABLE));
            return;
        }
        if(!mShowed.compareAndSet(false, true))
        {
            responseHandler.onComplete(DismissableAPIStatus.error, new Error(ErrorMap.UI_ACTIVITY_ALREADY_SHOWING));
            return;
        } else
        {
        	IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler Handler = new IMobageHttpResponseHandler.OnOpenUserProfileResponseHandler() {

                public final void onDismiss()
                {
                    Service.mShowed.set(false);
                    a.onComplete(DismissableAPIStatus.dismiss, null);
                }

                public final void onError(Error error)
                {
                    Service.mShowed.set(false);
                    a.onComplete(DismissableAPIStatus.error, error);
                }

                final IShowCommunityUICallback a;

            
            {
                a = responseHandler;
            }
            }
        	;
            MBUParcelableUser mbuparcelableuser = MBUParcelableUser.a(Mobage.getSession().getUser().toJson());
            LoadWebViewActivity.Load(activity, mbuparcelableuser, true, "GamesList", Handler);
            return;
        }
    }

    private static final String TAG = "Service";
    public static final int RESULT_CODE_SHOW_BANK_UI = 11000;
    static final AtomicBoolean mShowed; //a

    static 
    {
    	mShowed = Mobage.mShowed;
    }
}
